package com.devframe.web.util.json;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class JsonWapper<Container> {

	private boolean flag = true;

	private String msg = "success";

	protected Container datas;

	private HttpServletRequest request;

	private HttpServletResponse response;

	public JsonWapper() {
	}

	public JsonWapper(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}

	public static ObjectJsonWapper newObjectWapper() {
		return new ObjectJsonWapper();
	}

	public static ObjectJsonWapper newObjectWapper(HttpServletRequest request,
			HttpServletResponse response) {
		return new ObjectJsonWapper(request, response);
	}

	public static ListJsonWapper newListWapper() {
		return new ListJsonWapper();
	}

	public static ListJsonWapper newListWapper(HttpServletRequest request,
			HttpServletResponse response) {
		return new ListJsonWapper(request, response);
	}

	public static MapJsonWapper newMapWapper() {
		return new MapJsonWapper();
	}

	public static MapJsonWapper newMapWapper(HttpServletRequest request,
			HttpServletResponse response) {
		return new MapJsonWapper(request, response);
	}

	public static String toJSONString(Object o) {
		return JSON.toJSONString(o);
	}

	public static <T> T parseObject(String text, Class<T> clz) {
		return JSON.parseObject(text, clz);
	}
	public static <T> List<T> parseArray(String text, Class<T> clz) {
		return JSON.parseArray(text, clz);
	}

	public void out(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String encoding = request.getCharacterEncoding();
		response.setContentType("text/plain;charset=" + encoding);
		out(response.getWriter());
	}

	public void out(PrintWriter writer) throws IOException {
		JSONObject result = new JSONObject();
		result.put("flag", flag);
		result.put("msg", msg);
		if(datas!=null){
			result.put("data", datas);
		}
		
		
		writer.println(result.toString());
		writer.flush();
		// writer.close();
	}

	public JsonWapper<Container> setFlag(boolean flag) {
		this.flag = flag;
		return this;
	}

	public String getMsg() {
		return msg;
	}

	public JsonWapper<Container> setMsg(String msg) {
		this.msg = msg;
		return this;
	}

	public boolean isFlag() {
		return flag;
	}

	/**
	 * 
	 * <pre>
	 * <b>&nbsp;描述说明：待转为JSON的数据对象</b>
	 * </pre>
	 * 
	 * @author <b>詹保山(softzbs@126.com)</b>
	 * @param datas
	 * @return
	 */
	public JsonWapper<Container> transformDatas(Container datas) {
		this.datas = datas;
		return this;
	}

	/**
	 * 
	 * <pre>
	 * <b>&nbsp;描述说明：转为JSON的数据填充结束，将数据转为JSON输出</b>
	 * </pre>
	 * 
	 * @author <b>詹保山(softzbs@126.com)</b>
	 */
	public void end() {
		String encoding = request.getCharacterEncoding();
		response.setContentType("text/plain;charset=" + encoding);
		try {
			out(response.getWriter());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static class ObjectJsonWapper extends JsonWapper<Object> {
		public ObjectJsonWapper() {
		}

		public ObjectJsonWapper(HttpServletRequest request,
				HttpServletResponse response) {
			super(request, response);
		}

		public ObjectJsonWapper setObject(Object o) {
			datas = o;
			return this;
		}

	}

	public static class ListJsonWapper extends JsonWapper<List<Object>> {

		public ListJsonWapper() {
			datas = new ArrayList<Object>();
		}

		public ListJsonWapper(HttpServletRequest request,
				HttpServletResponse response) {
			super(request, response);
		}

		public ListJsonWapper add(Object o) {
			if (datas == null) {
				datas = new ArrayList<Object>();
			}
			datas.add(o);
			return this;
		}

		public ListJsonWapper addAll(List<? extends Object> list) {
			if (datas == null) {
				datas = new ArrayList<Object>();
			}
			datas.addAll(list);
			return this;
		}

		public ListJsonWapper setFlag(boolean flag) {
			super.setFlag(flag);
			return this;
		}

		public ListJsonWapper setMsg(String msg) {
			super.setMsg(msg);
			return this;
		}

		public ListJsonWapper transformDatas(List<Object> datas) {
			this.datas = datas;
			return this;
		}
	}

	public static class MapJsonWapper extends JsonWapper<Map<String, Object>> {
		public MapJsonWapper() {
			datas = new HashMap<String, Object>();
		}

		public MapJsonWapper(HttpServletRequest request,
				HttpServletResponse response) {
			super(request, response);
		}

		/**
		 * 
		 * <pre>
		 * <b>&nbsp;描述说明：填充数据</b>
		 * </pre>
		 * 
		 * @author <b>詹保山(softzbs@126.com)</b>
		 * @param key
		 * @param value
		 * @return
		 */
		public MapJsonWapper put(String key, Object value) {
			if (datas == null) {
				datas = new HashMap<String, Object>();
			}
			datas.put(key, value);
			return this;
		}

		public MapJsonWapper setFlag(boolean flag) {
			super.setFlag(flag);
			return this;
		}

		public MapJsonWapper setMsg(String msg) {
			super.setMsg(msg);
			return this;
		}

		public MapJsonWapper transformDatas(Map<String, Object> datas) {
			this.datas = datas;
			return this;
		}
		public void out(PrintWriter writer) throws IOException {
			JSONObject result = new JSONObject();
			result.put("flag", super.flag);
			result.put("msg", super.msg);
			if(datas!=null){
				result.putAll(datas);
			}
			
			writer.println(result.toString());
			writer.flush();
		}

	}

	public static void main(String[] args) throws IOException {
		// object
		{
			ObjectJsonWapper jsonWapper = JsonWapper.newObjectWapper();
			TestUser user = new TestUser();
			user.userId = "0001";
			user.userName = "username";
			jsonWapper.setObject(user);
			jsonWapper.out(new PrintWriter(System.out));
		}

		// list
		{
			ListJsonWapper jsonWapper = JsonWapper.newListWapper();
			{
				TestUser user = new TestUser();
				user.userId = "0001";
				user.userName = "username";
				jsonWapper.add(user);
			}
			{
				TestUser user = new TestUser();
				user.userId = "0002";
				user.userName = "name2";
				jsonWapper.add(user);
			}
			jsonWapper.out(new PrintWriter(System.out));
		}

		// map
		{
			MapJsonWapper jsonWapper = JsonWapper.newMapWapper();
			{
				TestUser user = new TestUser();
				user.userId = "0001";
				user.userName = "name1";
				jsonWapper.put(user.userId, user);
			}
			{
				TestUser user = new TestUser();
				user.userId = "0002";
				user.userName = "name2";
				jsonWapper.put(user.userId, user);
			}
			jsonWapper.out(new PrintWriter(System.out));
		}
	}

	public static class TestUser {
		String userId;

		String userName;

		public String getUserId() {
			return userId;
		}

		public void setUserId(String userId) {
			this.userId = userId;
		}

		public String getUserName() {
			return userName;
		}

		public void setUserName(String userName) {
			this.userName = userName;
		}
		
		
	}

}
